package demo1;

import javax.annotation.processing.Completion;
import java.util.Scanner;

public class TestDemo1 {
    public static final int LEN = 9;
    public int[] intOf(String s) {
        int[] arr = new int[2000];
        int len = LEN;
        int n = 0;
        int index = 0;
        for (int i = 0; i < s.length() / len; i++) {//循环次数
            n = 0;
            for (int j = s.length() -  i * len - len; j < s.length() - i * len; j++) {//以 pow(10,len) 为单位长度
                n = n * 10 + s.charAt(j) - '0';
            }
            arr[index++] = n;
        }
        n = 0;
        for (int i = 0; i < s.length() % len; i++) {
            n = n * 10 + s.charAt(i) - '0';
        }
        if(n != 0) {
            arr[index++] = n;
        }
        arr[arr.length - 1] = index;//末位为该数组有效长度
        return arr;
    }
    //补全字符 补0
    public static StringBuilder getStrBul(int n) {
        StringBuilder s = new StringBuilder(String.valueOf(n));
        s = s.reverse();
        int len = LEN - s.length();
        for (int i = 0; i < len; i++) {
            s = s.append("0");
        }
        return s.reverse();
    }
    public String addStrings(String num1, String num2) {
        int[] nums1 = intOf(num1);
        int[] nums2 = intOf(num2);
        int max = nums1[nums1.length - 1] > nums2[nums2.length - 1] ? nums1[nums1.length - 1] : nums2[nums2.length - 1];
        if(max == 0) {
            return "0";
        }
        int[] ret = new int[max + 1];//获得较长的有效长度 ret[max] 恰好是最后一个的下标
        int unit = (int)Math.pow(10, LEN);
        int base = 0;
        for (int i = 0; i <= max; i++) {
            ret[i] = (nums1[i] + nums2[i] + base) % unit;
            base = (nums1[i] + nums2[i] + base) / unit;
        }
        //此时是较大者的运算 i 表示较大者数组的下标
        StringBuilder retStr = new StringBuilder();
        boolean flag = ret[max] != 0;//ture 表示要考虑最后一位 false 表示不用考虑
        for (int i = 0; i < max - 1; i++) {
            retStr = getStrBul(ret[i]).append(retStr);
        }
        if(flag) {
            retStr = getStrBul(ret[max - 1]).append(retStr);
            retStr = new StringBuilder(String.valueOf(ret[max])).append(retStr);
        } else {
            retStr = new StringBuilder(String.valueOf(ret[max - 1])).append(retStr);
        }
        return String.valueOf(retStr) ;
    }
    public String addStrings1(String num1, String num2) {
        StringBuilder s1 = new StringBuilder(num1);
        StringBuilder s2 = new StringBuilder(num2);
        StringBuilder add = new StringBuilder();
        if(num2.length() > num1.length()) {
            for (int i = 0; i < num2.length() - num1.length(); i++) {
                add.append(0);
            }
            s1 = add.append(s1);
        } else {
            for (int i = 0; i < num1.length() - num2.length(); i++) {
                add.append(0);
            }
            s2 = add.append(s2);
        }
        int maxLen = s2.length();
        StringBuilder ret = new StringBuilder();
        int base = 0;
        for (int i = maxLen - 1; i >= 0; i--) {
            int n = (s1.charAt(i) + s2.charAt(i) + base - 2 * '0') % 10;
            base = (s1.charAt(i) + s2.charAt(i) + base - 2 * '0') / 10;
            ret.append(n);
        }
        if(base == 1) {
            ret.append(base);
        }
        return ret.reverse().toString();
    }

    public static void main(String[] args) {
        TestDemo1 testDemo1 = new TestDemo1();
        System.out.println(testDemo1.addStrings1("987654321", "123456789"));

    }
    public boolean isTrue(char c) {
        return !Character.isDigit(c) && !Character.isUpperCase(c) && !Character.isLowerCase(c);
    }
    public boolean isPalindrome(String s1) {
        String  s = s1.toLowerCase();
        int l = 0;
        int r = s.length() - 1;

        while(l < r) {
            while(l < r && isTrue(s.charAt(l))) {//既不是字母也不是数字那循环
                l++;
            }
            while(l < r && isTrue(s.charAt(r))) {
                r--;
            }
            if(s.charAt(l) != s.charAt(r)) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }

    public static void main4(String[] args) {
        String s = ".,";
        TestDemo1 testDemo1 = new TestDemo1();
        boolean b = testDemo1.isPalindrome(s);

    }

    public static int lastStrLen(String s) {
        int len = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            if(s.charAt(i) != ' ') {
                len++;
            } else {
                return len;
            }
        }
        return len;
    }

    public static void main3(String[] args) {
        String s;
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            s = in.nextLine();
            System.out.println(lastStrLen(s));
        }

    }

    public static int findIndex(String str) {
        int[] arr = new int[26];
        for (int i = 0; i < str.length(); i++) {
            int index = str.charAt(i);
            if(arr[index - 'a'] == 1) {
                return index;
            }
            arr[index - 'a'] += 1;
        }
        return -1;
    }
    public static void main2(String[] args) {
        String s = "bcdeaa";
        int ret = findIndex(s);
        if(ret > 0)
            System.out.println(String.valueOf(ret));
        else
            System.out.println("No find");
    }
    public static void main1(String[] args) {
        /*
         * 两者都是可以改变的
         * 不过StringBuffer 是只能单线程（应该是这么说）,安全性相对较高, 时间耗费较长
         * 而 StringBuilder 可以多线程，安全性较低，时间耗费较少
         * 它们两者与 String 的区别是
         * 前两者是可以改变对象的，而最后一个是不可以改变的
         * 如果 两个 String 相加，这的确可以实现拼接功能，但是本质还是使用 append()这方法
         * 并且每次拼接都会产生新的对象，需要对象的创建与销毁，所以时间很长，不建议用此方法
         **/
        StringBuffer buffer = new StringBuffer();
        String s1 = "123";
        String s2 = "456";
        buffer.append(s1);
        buffer.append(s2);
        System.out.println(buffer);
        StringBuilder builder = new StringBuilder();
        builder.append(s1);
        builder.append(s2);
        System.out.println(builder);
    }
}
